package com.loveprogrammer.eternity.skill.support.skills;

import com.loveprogrammer.eternity.enums.AttackTypeEnum;
import com.loveprogrammer.eternity.enums.WeaponTypeEnum;
import com.loveprogrammer.eternity.model.Character;
import com.loveprogrammer.eternity.model.Monster;
import com.loveprogrammer.eternity.common.Tuple;
import com.loveprogrammer.eternity.model.attack.AttackEntity;
import com.loveprogrammer.eternity.model.attack.AttackResult;
import com.loveprogrammer.eternity.model.product.Equipment;
import com.loveprogrammer.eternity.skill.support.states.ArmourReductionState;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @version 1.0.0
 * @description: 烈枪 火属性
 * @author: eric
 * @date: 2022-08-11 16:49
 **/
public class SkillFierceGun extends AbstractSkill {

    public SkillFierceGun(int id, int baseInterval, int level, int order, int maxLevel, String name, String desc) {
        super(id, baseInterval, level, order, maxLevel, name, desc);
    }

    @Override
    public boolean check(Character character) {
        if (character instanceof Monster) {
            return true;
        }
        // 检查是否持有枪类武器
        Equipment weapon = character.getEquips().getWeapon();
        return weapon != null
                && weapon.getWeaponTypeEnum() != null
                && weapon.getWeaponTypeEnum().equals(WeaponTypeEnum.GUN);
    }

    @Override
    public int opportunityPassive() {
        // 0为 主动技能
        return 0;
    }

    @Override
    public boolean checkLevelUp(int characterLevel) {
        // 每三级升一级
        if (characterLevel % 3 == 0) {
            return true;
        }
        return false;
    }

    /***
     * 发动技能
     **/
    @Override
    public Tuple<String, List<Character>> skillLaunch(Character character,
                                                      List<? extends Character> attack,
                                                      List<? extends Character> defences) {
        Tuple<String, List<Character>> result = new Tuple<>();

        List<Character> characters = new ArrayList<>();
        // 按照血量排序，攻击血量最低的
        Character defence = findSingleAttacker(defences,true);

        // 首先是普通攻击
        StringBuilder builder = new StringBuilder();
        builder.append(character.getName()).append("(").append(character.getCurrentHp()).append("/")
                .append(character.getHpMax()).append(")").append(" 使用技能[")
                .append(getName()).append(" ").append(getLevel()).append("级】攻击了 ").append(defence.getName());

        int power = getHarm(character);
        boolean crit = critCheck(character);
        if (crit) {
            power = critPower(power);
        }
        AttackResult attackResult = defence.defence(new AttackEntity(this, power, AttackTypeEnum.FLAME));
        computeHarm(builder,crit,defence,attackResult, character, attack, defences);

        int reduce = getSecondHarm(character);
        // 减甲状态
        ArmourReductionState armourReductionState = new ArmourReductionState(defence, defences, reduce);
        boolean state = defence.acquireState(armourReductionState, 10);
        // 施展成功 启动一个定时器，10秒钟
        if (state) {
            defence.setArmature(defence.getArmature() - reduce);
            // 持续10s
            timer.newTimeout(timeout -> armourReductionState.run(), 10, TimeUnit.SECONDS);
            // setTimeTask(armourReductionState, 10);
            characters.add(defence);
            builder.append(defence.getName()).append("进入减甲状态，减少护甲").append(reduce).append("\n");
        }
        result.setValue(characters);
        result.setKey(builder.toString());
        return result;
    }

    @Override
    protected int computeHarm(Character character) {
        return (int) (character.getStrength() * ((100 + 20 + getLevel() * 1.1) / 100) +
                character.getTechnique() * ((100 + 20 + getLevel() * 1.1) / 100));
    }

    @Override
    protected int computeSecondHarm(Character character) {
        return (int) (character.getTechnique() * 0.3 * ((100 + getLevel() * 5) / 100));
    }
}
